<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');

include_once('file.common.php');

if (!$_GET && $_ENV['QUERY_STRING']) {
	$_GET = array();
	parse_str($_ENV['QUERY_STRING'], $_GET);
}

function stampOid($content) {
	global $to;
	include_once('lib.sql.php');
	sqlStdConnect();
	$row = sqlGet("SELECT oid FROM user WHERE email=" . qq($to));
	sqlDisconnect();
	if ($row['oid']) {
		$content = preg_replace('/@@oid@@/', $row['oid'], $content);
	}
	return $content;
}

function writeit($filename, $content, $mode) {
	return;
	$h = fopen($filename, $mode);
	if ($h) {
		flock($h, LOCK_EX);
		fwrite($h, $content);
		flock($h, LOCK_UN);
		fclose($h);
	}
}

function fullLogName($name) {
	return $name;
}

// REQUIRED
$to = req('to');
$from = req('from');
$subject = req('subject');
$content = req('content');
$textonly = req('textonly', 'THIS EMAIL REQUIRES HTML VIEWER');
$sendToMeAlso = req('sendToMeAlso', FALSE);
$callback = req('callback', 'noCallbackName');

$sendToMeAlso = FALSE; // Force this off

// OPTIONAL
$sendfile = req('sendfile');
$recipients = req('recipients');
$toAll = req('toAll', FALSE);

// INTERNAL
$sentTo = array();
$logfilename = 'sendlog.txt';
$mailSent = FALSE;
$recipients = fullLogName($recipients);

if ($sendfile) {
	$content = file_get_contents($sendfile);
}

if (!$textonly && file_exists('textonly.txt')) {
	$textonly = file_get_contents('textonly.txt');
}

if ($toAll === TRUE && $recipients && file_exists($recipients)) {
	$recip = file_get_contents($recipients);
	$toArr = explode(';', $recip);
} else {
	$toArr = array($to);
	// Uncomment to test a bunch of sends
	//for ($i = 1;$i<100;$i++) {
	//	$toArr[] = $to;
	//}
}

$contentSafe = $content;
//$content = stampOid($content);

// To force send to me uncomment this line
//$toArr[] = 'nsbawden@gmail.com'; // Appends this address to sending addresses

$boundry = '----=_NextPart_000_0001_01C9143C.FF824310';
$nextpart = "--$boundry\r\n";
$endpart = "\r\n--$boundry--";


if (count($toArr) > 0 && $content && $subject) {
	$headers = $body = '';
	$headers .= "From: " . $from . "\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: multipart/alternative;\r\n";
	$headers .= "\tboundary=\"$boundry\"\r\n";
	$headers .= "X-Mailer: CYEPAD MAILER 1.1";

	$body .= $nextpart;
	$body .= "Content-Type: text/plain;\r\n";
	$body .= "\r\n";
	$body .= $textonly . "\r\n";

	$body .= $nextpart;
	$body .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\n";
	//$body .= "Content-Transfer-Encoding: quoted-printable\r\n";
	$body .= "\r\n";
	$body .= $content;
	$body .= $endpart;

	for ($i = 0; $i < count($toArr); $i++) {
		$toThis = trim($toArr[$i]);
		// valid email must have at least 6 chars (as in a@b.tv)
		if (strlen($toThis) > 5) {
			$mailSent = mail($toThis, $subject, $body, $headers);
			$sentTo[] = $toThis;
//				if ($mailSent === TRUE) myLog('SENT TO', $to);
//				else  myLog('FAILED SENDING TO', $to);
		}
	}
}

if ($sendToMeAlso && $content && $subject) {
	$headers = $body = '';
	$headers .= "From: " . $from . "\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: multipart/alternative;\r\n";
	$headers .= "\tboundary=\"$boundry\"\r\n";
	$headers .= "X-Mailer: CYEPAD MAILER 1.1";

	$body .= $nextpart;
	$body .= "Content-Type: text/plain;\r\n";
	$body .= "\r\n";
	$body .= $textonly . "\r\n";

	$body .= $nextpart;
	$body .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\n";
	//$body .= "Content-Transfer-Encoding: quoted-printable\r\n";
	$body .= "\r\n";
	$body .= $contentSafe;
	$body .= $endpart;

	$mailSent = mail($from, $subject, $body, $headers);
	$sentTo[] = $from;
}

if ($callback)
	echo "$callback({\n";
else
	echo "{\n";
	echo "\"result\":" . json_encode($mailSent) . ",\n";
	echo "\"date\":" . json_encode(date('Y-m-d H:i:s T')) . ",\n";
	echo "\"from\":" . json_encode($from) . ",\n";
	echo "\"sentTo\":" . json_encode(print_r($sentTo, true)) . ",\n";
	echo "\"subject\":" . json_encode($subject) . ",\n";
	echo "\"sendfile\":" . json_encode($sendfile) . ",\n";
	echo "\"sendToMeAlso\":" . json_encode($sendToMeAlso) . ",\n";
	echo "\"headers\":" . json_encode($headers) . ",\n";
	echo "\"content\":" . json_encode($content) . ",\n";
	echo "\"end\": true\n";
if ($callback)
	echo "\n})";
else
	echo "\n}";

?>